﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using Frame.Lib.Common;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;

namespace Frame.Lib.Core.Tree.Dir
{
    public class DirBO:TreeBO<DirInfo>
    {
        private DirDAL _dir;
        private string _tableName;

        public DirBO(string tableName)
            : base(tableName)
        {
            _dir = new DirDAL(tableName);
            this._tableName = tableName;
        }

        #region 带权限控制
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tv"></param>
        /// <param name="userID"></param>
        /// <param name="rightName"></param>
        public void BindTreeView(TreeView tv, List<int> lstBindIDs)
        {
            DataTable dt = base.GetDataTable();
            if (dt.Rows.Count == 0)//增加顶级目录,创建都就是登录用户
            {
                return;
            }

            if (AppSession.UserID != Constants.ADMIN_ID)//如非超级管理员，则进行权限判断
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int id = (int)dt.Rows[i]["ID"];

                    if (lstBindIDs.IndexOf(id) < 0)
                    {
                        dt.Rows.RemoveAt(i);
                        i--;
                    }
                }
            }
            tv.ShowLines = true;
            tv.Nodes.Clear();
            GetNode(tv, dt, null, 0);
        }
        #endregion

        private void GetNode(TreeView tv, DataTable dt, TreeNode node, int upperID)
        {
            tv.NodeStyle.ImageUrl = "";
            string strImageUrl = GlobalVar.WebAppPath + "common/images/Common/folder.gif";

            DataView dv = new DataView(dt);
            dv.RowFilter = "UpperID=" + upperID;
            dv.Sort = "Orders";
            foreach (DataRowView dr in dv)
            {
                TreeNode ndTmp = new TreeNode();
                ndTmp.Text = "<span onclick='SelectNode(\"img" + dr["ID"] + "\")'> <img id = 'img" + dr["ID"] + "'  alt='' style='border-width: 0;' align='absmiddle' src='" + strImageUrl + "' /> " + dr["Name"].ToString() + "</span>";
                ndTmp.ToolTip = dr["Name"].ToString();
                ndTmp.Value = dr["ID"].ToString();
                ndTmp.NavigateUrl = "Document/List.aspx?DirID=" + StrUtil.EncryptUrl(dr["ID"]);
                if (node == null)
                {
                    tv.Nodes.Add(ndTmp);
                }
                else
                {
                    node.ChildNodes.Add(ndTmp);
                }
                GetNode(tv, dt, ndTmp, (int)dr["ID"]);
            }
        }

        public override DataTable GetDataTable(object upperID)
        {
            return _dir.GetDataTable(upperID);
        }

        public Dictionary<int, int> GetCreatorIDs(List<int> ids)
        {
            Dictionary<int, int> lstCreatorIDs = _dir.GetCreatorIDs(ids);
            return lstCreatorIDs;
        }

      
    }
}
